import base64
import pickle

from apps.goods.models import SKU


def get_breadcrumb(cat3):
    cat2 = cat3.parent
    cat1 = cat2.parent
    breadcrumb={
        'cat1':{
            'url':cat1.goodschannel_set.all()[0].url,
            'name':cat1
        },
        'cat2':cat2,
        'cat3':cat3,
    }
    return breadcrumb

def get_hots(category_id):
    skus = SKU.objects.filter(category_id=category_id, is_launched=True).order_by('-sales')[:2]

    hot_skus = []
    for sku in skus:
        hot_skus.append({
            'id': sku.id,
            'default_image_url': sku.default_image.url,
            'name': sku.name,
            'price': sku.price
        })
    return hot_skus

class CookieSecret(object):
    @classmethod
    def dumps(cls,data):
        data=pickle.dumps(data)
        data= base64.b64encode(data)
        data=data.decode()
        return data

    @classmethod
    def loads(cls,data):

        data=base64.b64decode(data)
        data = pickle.loads(data)
        return data
